import Collapse from 'components/Markdown/Collapse'

export const meta = {
  title: 'GraphQL Requests (TypeScript)',
  position: 100,
  technology: "typescript",
  technologyOrder: 2,
  articleGroup: "GraphQL Requests",
}

## Overview

The Prisma client lets you send GraphQL queries and mutations directly to your Prisma service using the `$request` method:

```ts
$request: <T = any>(query: string, variables?: {[key: string]: any}) => Promise<T>;
```

As the GraphQL operation is passed untyped (as a regular string), the type of the returned promise is `any`.

### Examples

_Fetching a single user_:

```js
const query = `
  query {
    user(id: "cjcdi63j80adw0146z7r59bn5") {
      id
      name
    }
  }
`

const result: any = prisma.$request(query)
// sample result:
// {"data": { "user": { "id": "cjcdi63j80adw0146z7r59bn5", "name": "Sarah" } } }
```


_Fetching a single user using variables_:

```js
const query = `
  query ($id: ID!){
    user(id: $id) {
      id
      name
    }
  }
`

const variables: UserWhereUniqueInput = { id: 'cjcdi63j80adw0146z7r59bn5' }

const result: any = prisma.$request(query, variables)
// sample result:
// {"data": { "user": { "id": "cjcdi63j80adw0146z7r59bn5", "name": "Sarah" } } }
```

_Creating a new user_:

```js
const mutation = `
  mutation ($name: String!){
    createUser(name: $name) {
      id
      name
    }
  }
`

const variables: UserCreateInput = { name: 'Alice' }

const result: any = prisma.$request(mutation, variables)

// sample result:
// {"data": { "createUser": { "id": "cjlhqfbfa003t0a23rhzjragl", "name": "Alice" } } }
```
